package it.codelair.bakekaservice.Utilities;

import java.security.Key;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

import android.util.Base64;


public class AES {
	 private static final String ALGORITHM = "AES";
	    private static final byte[] keyValue = 
	        new byte[] { 'a','9','s','k','d','8','s','0','l','e','4','n','5','g','y','d','6','s','c','m','5','7','g','d','8','a','0','z','8','v','d','f' };

	     public static String encrypt(String valueToEnc) throws Exception {
	        Key key = generateKey();
	        Cipher c = Cipher.getInstance(ALGORITHM);
	        c.init(Cipher.ENCRYPT_MODE, key);
	        byte[] encValue = c.doFinal(valueToEnc.getBytes());
	        String encryptedValue =  Base64.encodeToString(encValue, Base64.DEFAULT);
	        return encryptedValue;
	    }

	    public static String decrypt(String encryptedValue) throws Exception {
	        Key key = generateKey();
	        Cipher c = Cipher.getInstance(ALGORITHM);
	        c.init(Cipher.DECRYPT_MODE, key);
	        byte[] decordedValue = Base64.decode(encryptedValue, Base64.DEFAULT);
	        byte[] decValue = c.doFinal(decordedValue);
	        String decryptedValue = new String(decValue);
	        return decryptedValue;
	    }

	    private static Key generateKey() throws Exception {
	        Key key = new SecretKeySpec(keyValue, ALGORITHM);
	        // SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM);
	        // key = keyFactory.generateSecret(new DESKeySpec(keyValue));
	        return key;
	    }
}